package study.javaee.practice.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootVersion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import study.javaee.practice.common.annotation.NoSwaggerDoc;
import study.javaee.practice.property.SwaggerProperties;

/**
 * Swagger API文档的配置
 */
@EnableOpenApi
@Configuration
public class SwaggerConfig {

    private final SwaggerProperties swaggerProperties;

    @Autowired
    public SwaggerConfig(SwaggerProperties swaggerProperties) {
        this.swaggerProperties = swaggerProperties;
    }

    @Bean
    public Docket getDocket() {
        Docket docket = new Docket(DocumentationType.OAS_30);
        return docket
                .enable(swaggerProperties.getEnable())
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("study.javaee.practice.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(NoSwaggerDoc.class).negate())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(swaggerProperties.getAppName() + " Api Doc")
                .description(swaggerProperties.getAppDescription())
                .contact(new Contact(swaggerProperties.getContactName(), swaggerProperties.getContactUrl(),
                        swaggerProperties.getContactEmail()))
                .version("Application Version: " + swaggerProperties.getAppVersion() + ", Spring Boot Version: "
                        + SpringBootVersion.getVersion())
                .build();
    }


}
